﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net.Mail;

namespace Common {
  /// <summary>
  /// Mail helper class
  /// </summary>
  public static class MailHelper {

    /// <summary>
    /// Internets the connection available.
    /// </summary>
    /// <returns></returns>
    public static bool InternetConnectionAvailable( ) {
      bool result = false;
      Ping ping = new Ping( );
      PingReply reply = null;
      try {
        reply = ping.Send( "www.google.com" );
        result = reply.Status == IPStatus.Success;
      } catch ( Exception ) {

      }

      return result;
    }

    /// <summary>
    /// Sends the mail.
    /// </summary>
    /// <param name="mailFromAddress">The mail from address.</param>
    /// <param name="mailToAddress">The mail to address.</param>
    /// <param name="subject">The subject.</param>
    /// <param name="body">The body.</param>
    /// <param name="smtpServerName">Name of the SMTP server.</param>
    /// <param name="port">The port.</param>
    /// <param name="username">The username.</param>
    /// <param name="password">The password.</param>
    public static void SendMail( string mailFromAddress, string mailToAddress, string subject, string body,
      string smtpServerName, int port, string username, string password ) {

      MailMessage message = new MailMessage( );
      message.To.Add( mailToAddress );
      message.From = new MailAddress( mailFromAddress );
      message.Subject = subject;
      message.Body = body;
      SmtpClient smtp = new SmtpClient( smtpServerName , port );
      smtp.Credentials = new System.Net.NetworkCredential( username, password );
      smtp.EnableSsl = true;
      smtp.Send( message );

    }


  }
}
